<?php
namespace app\api\controller;
use think\Lang;

/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 卖家咨询控制器
 */
class Sellerconsult extends MobileSeller
{
    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        Lang::load(APP_PATH . 'home/lang/'.config('default_lang').'/sellerconsult.lang.php');
    }


    /**
     * @api {POST} api/Sellerconsult/index 商品咨询列表页
     * @apiVersion 1.0.0
     * @apiGroup Sellerconsult
     *
     * @apiHeader {String} X-DS-KEY 卖家授权token
     *
     * @apiParam {String} type 回复状态 to_reply待回复 replied已回复
     * @apiParam {Int} ctid 咨询类型
     * @apiParam {String} page 页码
     * @apiParam {String} pagesize 每页显示数量
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {Object[]} result.consult_list  咨询列表 （参考字段参考consult表）
     * @apiSuccess {Object} result.consult_type  咨询类型，键为咨询类型ID
     * @apiSuccess {Int} result.consult_type.consulttype_id  咨询类型ID
     * @apiSuccess {String} result.consult_type.consulttype_introduce  咨询类型描述
     * @apiSuccess {String} result.consult_type.consulttype_name  咨询类型名称
     * @apiSuccess {Int} result.consult_type.consulttype_sort  咨询类型排序
     * @apiSuccess {Int} result.page_total  总页数
     * @apiSuccess {Boolean} result.hasmore  是否有更多 true是false否
     */
    public function index()
    {
        $consult_model = model('consult');
        $list_consult = array();
        $where = array();
        if (trim(input('param.type')) == 'to_reply') {
            $where['consult_reply'] = array('eq', '');
        }
        elseif (trim(input('param.type')) == 'replied') {
            $where['consult_reply'] = array('neq', '');
        }
        if (intval(input('param.ctid')) > 0) {
            $where['consulttype_id'] = intval(input('param.ctid'));
        }
        $where['store_id'] = $this->store_info['store_id'];

        $list_consult = $consult_model->getConsultList($where, '*', 0);
        // 咨询类型
        $consult_type = rkcache('consulttype', true);
        
        $result= array_merge(array('consult_list' => $list_consult,'consult_type'=>$consult_type), mobile_page($consult_model->page_info));
        ds_json_encode(10000, '',$result);
    }


    /**
     * @api {POST} api/Sellerconsult/drop_consult 商品咨询删除处理
     * @apiVersion 1.0.0
     * @apiGroup Sellerconsult
     *
     * @apiHeader {String} X-DS-KEY 卖家授权token
     *
     * @apiParam {Int} id 咨询ID
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     */
    public function drop_consult()
    {
        $ids = trim(input('param.id'));
        if ($ids < 0) {
            ds_json_encode(10001,lang('param_error'));
        }
        $consult_model = model('consult');
        $id_array = explode(',', $ids);
        $where = array();
        $where['store_id'] = $this->store_info['store_id'];
        $where['consult_id'] = array('in', $id_array);
        $state = $consult_model->delConsult($where);
        if ($state) {
            ds_json_encode(10000,lang('store_consult_drop_success'));
        }
        else {
            ds_json_encode(10001,lang('store_consult_drop_fail'));
        }
    }



    /**
     * @api {POST} api/Sellerconsult/reply_save 商品咨询回复内容的保存处理
     * @apiVersion 1.0.0
     * @apiGroup Sellerconsult
     *
     * @apiHeader {String} X-DS-KEY 卖家授权token
     *
     * @apiParam {Int} consult_id 咨询ID
     * @apiHeader {String} content 回复内容
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     */
    public function reply_save()
    {
        $consult_id = intval(input('consult_id'));
        if ($consult_id <= 0) {
            ds_json_encode(10001,lang('param_error'));
        }
        $consult_model = model('consult');
        $update = array();
        $update['consult_reply'] = input('post.content');
        $condtion = array();
        $condtion['store_id'] = $this->store_info['store_id'];
        $condtion['consult_id'] = $consult_id;
        $state = $consult_model->editConsult($condtion, $update);
        if ($state) {
            $consult_info = $consult_model->getConsultInfo(array('consult_id' => $consult_id));
            // 发送用户消息
            $param = array();
            $param['code'] = 'consult_goods_reply';
            $param['member_id'] = $consult_info['member_id'];
            $param['param'] = array(
                'goods_name' => $consult_info['goods_name'], 'consult_url' => url('Memberconsult/my_consult')
            );
            \mall\queue\QueueClient::push('sendMemberMsg', $param);

            ds_json_encode(10000,lang('ds_common_op_succ'));
        }
        else {
            ds_json_encode(10001,lang('ds_common_op_fail'));
        }
    }


}